home *** CD-ROM | disk | FTP | other *** search
- #ifndef lint
- static char SccsId[]= "@(#)dragging.c V1.21 3/22/95";
- #endif
- /*
- | file name - dragging.c
- |===================================================================
- |
- | This example uses XORing to "drag" objects around. Select an
- | object with the LEFT or MIDDLE mouse button, drag the object
- | by holding the mouse button DOWN, when you release the button
- | the object stays in the new position.
- |
- | To quit the program select "q|Q" or the RIGHT mouse button.
- |
- |===================================================================
- */
- #include <windows.h>
-
- /* DV-Tools header files */
- #include "std.h" /* <stdio.h> etc., scalar & macro definitions */
- #include "dvstd.h" /* public types & constants */
- #include "dvtools.h" /* constants used by T routines */
- #include "dvGR.h" /* constants used by window mgt & GR routines */
- #include "VOstd.h" /* constants used by VO & VOob routines */
- #include "Tfundecl.h" /* T routines (screens, drawports & views) */
- #include "VOfundecl.h" /* VO routines (objects) */
- #include "VUerfundecl.h" /* VUer routines (event handling routines) */
- #include "GRfundecl.h" /* GR routines (interface to display device) */
-
-
- /* Constants */
- #define DVPATH (char *)NULL
- #define DISPFORMS_STB (char *)NULL
- #define DVDEVICE (char *)NULL
- #define DVCOLORTABLE (char *)NULL
- #define VIEW_NAME "move_objs.v"
- #define SCREEN_VIEWPORT (RECTANGLE *)NULL
- #define DRAWING_VIEWPORT (RECTANGLE *)NULL
-
- /* Define global variables */
- DRAWPORT drawport; /* how & where to display picture, picture frame */
- OBJECT move_object; /* object selected to be moved */
-
-
- /* Global forward function declarations */
- void MoveObject V_P_((DV_POINT *source, DV_POINT *dest));
-
-
- /*
- * MAIN PROGRAM
- */
- int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
- LPSTR lpCmdLine, int nCmdShow )
- {
- INT argc = 0;
- CHAR **argv;
-
- /*
- * program arguments
- * argv[1] - display device name (default is to use DVDEVICE)
- * argv[2] - view name (default is move_objs.v)
- */
-
- /* Define & initialize device name and view filename */
- char *device_name = DVDEVICE; /* default device name */
- char *view_name = VIEW_NAME; /* default view name */
-
- /* Define display variables */
- OBJECT screen; /* display device, the window */
- VIEW view; /* picture representation of the view file */
-
- /* Control loop variables */
- OBJECT location; /* the event representation */
-
- /* Variables used for rubberbanding */
- DV_POINT src_pt, /* start point or current point of object */
- dest_pt; /* position to move to */
- DV_POINT *world_pt; /* world coordinate point */
- int rubberbanding = NO, /* flag for rubberbanding */
- Quit = NO; /* flag to quit program */
-
- /*-----------------
- * Initialization
- *
- * TInit: perform the initialization of DV-Tools
- * TInit reads your configuration file and any
- * environment variables or logical names set.
- */
- make_argv(&argc,&argv,GetCommandLine());
- TInit( DVPATH, DISPFORMS_STB );
-
- /*
- * TscOpenSet: opens a device as a screen object using
- * specified attributes
- *
- * Set exposure block to YES to insure the window
- * is ready for drawing when TdpDraw is called.
- */
- if (argc > 1)
- device_name = argv[1];
- screen = TscOpenSet (device_name, DVCOLORTABLE,
- V_WINDOW_NAME, "XOR example",
- V_WINDOW_WIDTH, 600,
- V_WINDOW_HEIGHT, 600,
- V_X_EXPOSURE_BLOCK, YES,
- V_ACTIVE_CURSOR, V_END_OF_LIST);
- if (!screen)
- {
- printf ("Must specify device on command line or");
- printf (" in DataViews configuration file.\n");
- S_EXIT (EXIT_ERR);
- }
-
- /*
- * VOscWinEventMask: sets the screen's window event mask
- */
- VOscWinEventMask ((ULONG) V_KEYPRESS | V_BUTTONPRESS | V_BUTTONRELEASE
- | V_MOTIONNOTIFY | V_EXPOSE | V_RESIZE,
- (ULONG) 0);
-
- /*
- * TviLoad: Load a view in from a file, either a
- * user supplied view or default view file move_objs.v
- * TdpCreate: Create a DV-Tools window, a drawport.
- * The drawport is attached to the screen object
- * specified while view specifies the view to be
- * displayed on the screen.
- */
- if (argc == 3)
- view_name = argv[2];
- view = TviLoad (view_name);
- if (!view)
- {
- printf ("Could not load view from file ");
- printf ("%s.\n", view_name);
- S_EXIT (EXIT_ERR);
- }
- drawport = TdpCreate (screen, view, SCREEN_VIEWPORT, DRAWING_VIEWPORT);
-
- /*
- * TscErase: Erase the entire screen in the default
- * background color
- * TdpDraw: Draw the contents of the drawport
- */
- TscErase (screen);
- TdpDraw (drawport);
-
- /*--------------------
- * Control loop
- *
- * Poll the event queue for window events. If the key
- * represents the character 'q' or 'Q' then quit the program.
- * If user selects to drag an object using the left or middle
- * mouse buttons then wait for the buttonrelease before
- * dropping the object.
- */
- FOREVER
- {
- location = VOscWinEventPoll (V_WAIT);
- switch (VOloType (location))
- {
-
- case V_EXPOSE:
- /*
- * VOloRegion: Returns a rectangle representing the
- * exposed region on the screen.
- * TscRedraw: After erasing, redraws all the drawports
- * in the screen.
- * A portion of the window has been exposed and needs
- * to be redrawn.
- */
- TscRedraw (screen, VOloRegion (location));
- break;
-
- case V_RESIZE:
- /*
- * The window size has been changed.
- * TscReset: Resets all screen drawports after
- * window resizing
- */
- TscReset (screen);
- break;
-
- case V_KEYPRESS:
- /*
- * Check key selected.
- * VOloKeySym: Returns the key symbol value of the
- * location object
- * If the key symbol represents the characters 'q'
- * or 'Q' then quit the program.
- */
- switch (VOloKeySym (location))
- {
- case 'q':
- case 'Q':
- Quit = YES;
- break;
-
- default:
- break;
- }
- break;
-
- case V_BUTTONPRESS:
- /*
- * Check button selected.
- * VOloButton: Returns the button that was pressed
- * TloGetSelectedObject: Get the selected object
- *
- * The left/middle mouse button acts as the selection
- * button. The object selected will be moved to the
- * mouse position designated by the Buttonrelease.
- */
- switch (VOloButton (location))
- {
- case 1:
- if (!rubberbanding)
- {
- if ((move_object = TloGetSelectedObject (location)))
- {
- /*
- * VOloWcpGet: Returns the location object in drawing's
- * world coordinates.
- * GRset: Resets device attributes
- *
- * Erase the selected object, obtain the point information
- * used to select the object and set the drawing mode
- * to XOR. XOR draws by reversing bits and is applicable
- * for rubberbanding. Finally, draw the object with the
- * current drawing mode and set the rubberbanding flag
- * to YES.
- */
- world_pt = VOloWcpGet (location);
- if (world_pt != NULL)
- {
- TdpEraseObject (drawport, move_object);
- src_pt.x = world_pt->x;
- src_pt.y = world_pt->y;
- dest_pt.x = src_pt.x;
- dest_pt.y = src_pt.y;
- GRset (V_DRAW_FUNCTION, V_XOR, V_END_OF_LIST);
- TdpDrawObject (drawport, move_object);
- rubberbanding = YES;
- }
- }
- }
- break;
-
- case 3:
- Quit = YES;
- break;
- }
- break;
-
- case V_BUTTONRELEASE:
- /*
- * GRset: Resets device attributes
- * TdpDrawObject: Draw an object in a drawport
- *
- * Reset the drawing mode to V_COPY which represents the
- * normal drawing mode. Draw the object in it's current
- * position using the current drawing mode. Set the
- * the rubberbanding flag to NO.
- */
- if (rubberbanding == YES)
- {
- GRset (V_DRAW_FUNCTION, V_COPY, V_END_OF_LIST);
- TdpDrawObject (drawport, move_object);
- rubberbanding = NO;
- }
- break;
-
- case V_MOTIONNOTIFY:
- /*
- * VOloWcpGet: Returns the location object in drawing's
- * world coordinates.
- *
- * Make the previous point be the source point and copy
- * the current point to the destination point. Then call
- * the procedure to move the object.
- */
- if (rubberbanding == YES)
- {
- world_pt = VOloWcpGet (location);
- if (world_pt != NULL)
- {
- src_pt.x = dest_pt.x;
- src_pt.y = dest_pt.y;
- dest_pt.x = world_pt->x;
- dest_pt.y = world_pt->y;
-
- MoveObject (&src_pt, &dest_pt);
- }
- }
- break;
-
- default:
- break;
- }
-
- if (Quit == YES)
- break;
- }
-
- /*--------------------
- * Termination
- *
- * TscErase: Erase the entire screen in the default
- * background color
- * TdpDestroy: Destroy the drawport,
- * TviDestroy: Destroy the view, freeing the allocated memory
- * TscCloseCurrentScreen: Close the current display screen
- * TTerminate: Perform the clean-up for DV-Tools
- */
- TscErase (screen);
- TdpDestroy (drawport);
- TviDestroy (view);
- TscCloseCurrentScreen ();
- TTerminate ();
- return EXIT_OK;
- }
-
- void
- MoveObject (source, dest)
- DV_POINT *source;
- DV_POINT *dest;
- {
- OBJECT point;
- int i, delta_x, delta_y;
-
- /*
- * Determine the change between the start point and
- * the current destination point. This is the amount
- * that each control point belonging to the object
- * will be moved.
- */
- delta_x = dest->x - source->x;
- delta_y = dest->y - source->y;
-
- /*
- * TdpDrawObject: Draws an object in a drawport
- */
- TdpDrawObject (drawport, move_object);
-
- /*
- * VOobPtGet: Gets the specified control point of an object
- * VOptMove: Moves a point object
- *
- * Get each control object and move it by the calculated amount.
- */
- for (i = 1; i <= VOobPtGet (move_object, 0); i++)
- {
- point = VOobPtGet (move_object, i);
- VOptMove (point, DV_RELATIVE, delta_x, delta_y);
- }
- TdpDrawObject (drawport, move_object);
- }
-